package src.tree.level_order;

import src.tree.TreeNode;

import java.util.ArrayDeque;

public class no111 {//二叉树的最小深度

    public int minDepth(TreeNode root) {

        if (root == null) return 0;
        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        int depth = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            depth++;
            while (size-- > 0) {
                TreeNode node = queue.poll();
                //if (node.left == null && node.right == null) return depth;  //放在这行也行
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                //如果当前结点的左右孩子都空，即为深度最小的一层。直接返回，不管后面的了。break不行，跳出内层for 没出外层
                if (node.left == null && node.right == null) {
                    return depth;
                }
            }

        }
        return depth;
    }

}
